package org.dydl.service.system.impl;

import org.apache.commons.lang.StringUtils;
import org.dydl.common.dto.ResponseData;
import org.dydl.common.dto.ResponseMessage;
import org.dydl.dao.system.repository.ISysCustomerRepository;
import org.dydl.model.system.SysCustomer;
import org.dydl.service.system.ISysCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


@Service("customerService")
public class SysCustomerServiceImpl implements ISysCustomerService{
	
	@Autowired
	private ISysCustomerRepository sysCustomerRepository;

	@Override
	public ResponseMessage save(SysCustomer sysCustomer) {
		
		return new ResponseMessage(true, sysCustomerRepository.save(sysCustomer));
	}

	@Override
	public ResponseMessage delete(int id) {
		
		sysCustomerRepository.delete(id);
		
		return new ResponseMessage(true);
	}

	@Override
	public ResponseMessage getSysCustomerList(int page, int rows, String fullName) {
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<SysCustomer> pageList = null;
		if (StringUtils.isNotBlank(fullName)) {
			pageList = sysCustomerRepository.getSysCustomerListLike(String.format("%s%s%s", "%", StringUtils.trim(fullName), "%"),
					pageable);
		} else {
			pageList = sysCustomerRepository.getSysCustomerList(pageable);
		}
		return new ResponseMessage(true, new ResponseData<SysCustomer>(page, pageList.getTotalPages(),
				((int) pageList.getTotalElements()), pageList.getContent()));
		
	}

}
